package com.jic.manager.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.mapper.CmsModuleDataMapper;
import com.jic.manager.mapper.CmsModuleMapper;
import com.jic.manager.mapper.CmsModuleTemplateMapper;
import com.jic.manager.mapper.CmsPageMapper;
import com.jic.manager.entity.CmsModule;
import com.jic.manager.entity.CmsModuleData;
import com.jic.manager.entity.CmsModuleTemplate;
import com.jic.manager.entity.CmsPage;
import com.jic.conf.request.AddCmsModuleRequest;
import com.jic.conf.request.AddModuleDataRequest;
import com.jic.conf.request.CmsModuleTemplateQuery;
import com.jic.conf.request.GetModuleCmsConfRequest;
import com.jic.conf.request.SectionMobileRequest;
import com.jic.conf.request.rest.CmsModuleAndDataRequest;
import com.jic.conf.request.rest.GetCmsProductListRequest;
import com.jic.conf.response.CmsModuleDataResponse;
import com.jic.conf.response.CmsModuleResponse;
import com.jic.conf.response.CmsModuleTemplateResponse;
import com.jic.conf.response.rest.CmsGetIndexPageConfResponse;
import com.jic.conf.response.rest.GetCmsProductListResponse;
import com.jic.conf.response.rest.PageModuleResponse;
import com.jic.manager.service.CmsModuleDataService;
import com.jic.manager.service.CmsModuleService;
import com.jic.manager.service.CmsModuleTemplateService;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.rest.response.ProductResponseFront;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * cms栏目
 * 
 * @author :
 * @email :
 * @since : 2020-03-04 11:59:53
 * @version : v1.0.0
 */
@Service
@Slf4j
public class CmsModuleServiceImpl implements CmsModuleService {

	@Resource
	private CmsModuleMapper cmsModuleMapper;

	@Autowired
	private CmsModuleService cmsModuleService;

	@Autowired
	private CmsModuleTemplateService cmsModuleTemplateService;

	@Autowired
	private CmsModuleDataService cmsModuleDataService;
	@Resource
	private CmsModuleDataMapper cmsModuleDataMapper;
	@Resource
	private CmsPageMapper cmsPageMapper;
	@Resource
	private ProductListApi productListApi;
	@Resource
	private CmsModuleTemplateMapper cmsModuleTemplateMapper;

	@Override
	public long insert(CmsModule cmsModule) {
		long flag = cmsModuleMapper.insert(cmsModule);
		return flag;
	}

	@Override
	public long delete(Long id) {
		return cmsModuleMapper.delete(id);
	}

	@Override
	public long update(CmsModule cmsModule) {
		return cmsModuleMapper.update(cmsModule);
	}

	@Override
	public CmsModule queryByPrimaryKey(Long id) {
		return cmsModuleMapper.queryByPrimaryKey(id);
	}

	@Override
	public List<CmsModule> query(CmsModule cmsModule) {
		return cmsModuleMapper.query(cmsModule);
	}

	@Override
	public PageResult<CmsModule> queryPage(CmsModule cmsModule, Page pageQuery) {
		// 计算下标
		int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
		List<CmsModule> list = cmsModuleMapper.queryPage(startIndex, pageQuery.getLimit(), cmsModule);
		long count = cmsModuleMapper.queryPageCount(cmsModule);
		PageResult pageResult = new PageResult();
		pageResult.setRows(list);
		pageResult.setTotal(count);
		return pageResult;
	}

	@Override
	public List<CmsModuleResponse> query(Long cmsId, int refType) {
		List<CmsModuleResponse> cmsModuleResponses = new ArrayList<>();
		CmsModule cmsModule = new CmsModule();
		cmsModule.setCmsPageId(cmsId);
		// 普�?�页�
		cmsModule.setRefType(refType);
		cmsModule.setDeleteFlag(0);
		// 查询页面更下的所有栏�
		List<CmsModule> cmsModules = cmsModuleService.query(cmsModule);

		if (CollectionUtils.isNotEmpty(cmsModules)) {
			List<Long> cmsModelIds = new ArrayList<>();

			for (CmsModule bean : cmsModules) {
				cmsModelIds.add(bean.getId());
			}

			Map<Long, List<CmsModuleData>> cmsModuleDataMap = new HashMap<>();

			Map<Long, CmsModuleTemplate> cmsModuleTemplateMap = new HashMap<>();
			List<CmsModuleTemplate> cmsModuleTemplates = cmsModuleTemplateService.queryByModuleIds(cmsModelIds);

			if (CollectionUtils.isNotEmpty(cmsModuleTemplates)) {
				// 因为��关系，不�?要判断直接放入map
				for (CmsModuleTemplate entity : cmsModuleTemplates) {
					cmsModuleTemplateMap.put(entity.getCmsModuleId(), entity);
				}
			}

			// 查询栏目下的�?�
			List<CmsModuleData> cmsModuleDatas = cmsModuleDataService.queryByModuleIds(cmsModelIds);

			if (CollectionUtils.isNotEmpty(cmsModuleDatas)) {
				for (CmsModuleData entity : cmsModuleDatas) {

					List<CmsModuleData> lst = cmsModuleDataMap.get(entity.getCmsModuleId());
					if (lst == null) {
						lst = new ArrayList<>();
						lst.add(entity);
					} else {
						lst.add(entity);
					}

					cmsModuleDataMap.put(entity.getCmsModuleId(), lst);
				}
			}

			
			CmsModuleResponse cmsModuleResponse = null;
			CmsModuleDataResponse cmsModuleDataResponse = null;
			for (CmsModule bean : cmsModules) {
				cmsModuleResponse = new CmsModuleResponse();
				cmsModuleResponse.setId(bean.getId());
				cmsModuleResponse.setDataType(bean.getDataType());
				cmsModuleResponse.setName(bean.getName());
				cmsModuleResponse.setProductNums(bean.getProductNums());
				cmsModuleResponse.setSort(bean.getSort());

				// 取得商品信息
				List<CmsModuleData> lst = cmsModuleDataMap.get(bean.getId());
				if (CollectionUtils.isNotEmpty(lst)) {

					List<CmsModuleDataResponse> cmsModuleDataResponses = new ArrayList<>();

					for (CmsModuleData entity : lst) {
						cmsModuleDataResponse = new CmsModuleDataResponse();
						cmsModuleDataResponse.setCmsModuleId(entity.getCmsModuleId());
						cmsModuleDataResponse.setId(entity.getId());
						cmsModuleDataResponse.setIsOffShelve(entity.getIsOffShelve());
						cmsModuleDataResponse.setMpcode(entity.getMpcode());
						cmsModuleDataResponse.setMpId(entity.getMpId());
						cmsModuleDataResponse.setPromotionId(entity.getPromotionId());
						cmsModuleDataResponse.setSortValue(entity.getSortValue());
						cmsModuleDataResponses.add(cmsModuleDataResponse);
					}

					cmsModuleResponse.setCmsModuleData(cmsModuleDataResponses);
				}

				// 设置栏目模板信息
				CmsModuleTemplate cmsModuleTemplate = cmsModuleTemplateMap.get(bean.getId());

				if (cmsModuleTemplate != null) {

					CmsModuleTemplateResponse cmsModuleTemplateResponse = new CmsModuleTemplateResponse();
					cmsModuleTemplateResponse.setCmsModuleId(cmsModuleTemplate.getCmsModuleId());
					cmsModuleTemplateResponse.setCmsTemplateId(cmsModuleTemplate.getCmsModuleId());
					cmsModuleTemplateResponse.setCode(cmsModuleTemplate.getCode());
					cmsModuleTemplateResponse.setId(cmsModuleTemplate.getId());
					cmsModuleTemplateResponse.setName(cmsModuleTemplate.getName());
					cmsModuleTemplateResponse.setPreviewImg(cmsModuleTemplate.getPreviewImg());
					cmsModuleTemplateResponse.setVariables(cmsModuleTemplate.getVariables());
					cmsModuleResponse.setCmsModuleTemplate(cmsModuleTemplateResponse);
				}
				cmsModuleResponses.add(cmsModuleResponse);
			}
		}
		
		return cmsModuleResponses;
	}

	@Override
	public RestResult<CmsGetIndexPageConfResponse> getIndexCmsConf(CmsModuleAndDataRequest request) {
		try {
			//查询生效的page
			if (request==null || request.getPlatform()==null){
				throw new Exception("CMS配置平台类型为空");
			}
			CmsGetIndexPageConfResponse cmsGetIndexPageConfResponse = cmsPageMapper.queryInfoByStatus(request);
			//查询module list
			if (null == cmsGetIndexPageConfResponse){
				return RestResult.success(null);
			}
			List<PageModuleResponse> pageModuleResponseList = cmsModuleMapper.queryListByCmsPageId(cmsGetIndexPageConfResponse.getPageId());
			pageModuleResponseList.forEach(pageModuleResponse -> {
				if (StringUtils.isNotEmpty(pageModuleResponse.getVariables())){
					// 如是轮播模板则校验模板有效期
					if (pageModuleResponse.getCode().equals("WX_BANNER_CODE") ||
							pageModuleResponse.getCode().equals("MULTI_IMAGE_CODE")){
						JSONArray variables = JSON.parseArray(pageModuleResponse.getVariables());
						JSONArray valid = new JSONArray();
						// 遍历模板数据判断有效期
						for (Object object : variables){
							JSONObject o = JSONObject.fromObject(object);
							// 设置了有效期
							if (StringUtils.isNotBlank(o.getString("vaildDate"))){
								SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
								try {
									Long startDate = sdf.parse(o.getString("startDate")).getTime();
									Long endDate = sdf.parse(o.getString("endDate")).getTime();
									Long currentDate = new Date().getTime();
									// 获取有效的轮播图 当前时间大于开始时间 并且 小于结束时间
									if (currentDate >= startDate && currentDate <= endDate){
										valid.add(object);
									}
								} catch (ParseException e) {
									log.error("parse Date is error",e);
								}
							}else {
								valid.add(object);
							}
						}

						pageModuleResponse.setVariablesObject(JSONArray.parseObject(valid.toString(),Object.class));
					}else {
						pageModuleResponse.setVariablesObject(JSONArray.parseObject(pageModuleResponse.getVariables(),Object.class));
					}

				}
			});
			cmsGetIndexPageConfResponse.setPageModuleResponseList(pageModuleResponseList);
			return RestResult.success(cmsGetIndexPageConfResponse);
		}catch (Exception e){
			log.error("查询H5Cms配置失败============>>>>>{}",e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}



	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@Override
	public List<com.jic.manager.entity.vo.response.CmsModuleResponse> listCmsModuleRecordPagination(com.jic.manager.entity.vo.request.CmsModuleRequest request) {
		//分页处理

		CmsModule param = new CmsModule();
		BeanUtils.copyProperties(request, param);
		param.setDeleteFlag(0);
		List<CmsModule> list = cmsModuleMapper.listCmsModuleRecordPagination(request.getStart(), request.getLimit(), param);

		long count = cmsModuleMapper.listPageCount(param);

		List<CmsModuleResponse> schoolResponseList = list.stream().map(temp -> {
			CmsModuleResponse schoolResponse = new CmsModuleResponse();
			BeanUtils.copyProperties(temp, schoolResponse);
			return schoolResponse;
		}).filter(item -> item != null).collect(Collectors.toList());

		PageResult<CmsModuleResponse> pageResult = new PageResult<>();
		pageResult.setRows(schoolResponseList);
		pageResult.setTotal(count);

		return new ArrayList<>();
	}

	/**
	 * 分页查询，获取�?�记�
	 *
	 * @param request
	 * @return
	 */
	@Override
	public long listPageCount(CmsModule request) {
		return cmsModuleMapper.listPageCount(request);
	}


	/**
	 * 不分页查�
	 *
	 * @param request
	 * @return
	 */
	@Override
	public List<CmsModule> listCmsModuleAllRecord(CmsModule request) {
		//分页处理
		request.setDeleteFlag(0);
		return cmsModuleMapper.selectBySelective(request);
	}

	/**
	 * 修改审核
	 *
	 * @param request
	 * @return
	 */
	@Override
	public long updateCmsModule(CmsModule request) throws Throwable {
		try {
			log.info("修改记录");
			CmsModule school = new CmsModule();
			BeanUtils.copyProperties(request, school);
			long flag = cmsModuleMapper.updateByPrimaryKey(school);
		} catch (Exception e) {
			log.error("记录审核失败", e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改异常");
		}

		return -1;
	}

	@Override
	public RestResult<List<GetCmsProductListResponse>> getCmsProductList(GetCmsProductListRequest getCmsProductListRequest) {
		try {
			List<GetCmsProductListResponse> rest = new ArrayList<>();
			//根据模块二Id
			List<Long> productIds = cmsModuleMapper.queryProductList(getCmsProductListRequest.getModuleId(),getCmsProductListRequest.getType());
			if (CollectionUtils.isEmpty(productIds)){
				return RestResult.success(rest);
			}
			log.info("=====查询商品Ids===>>>>>{}",productIds);
			RestResult<List<ProductResponseFront>> listRestResult = productListApi.queryProductListByIds(productIds);

			List<ProductResponseFront> productResponseFrontList = listRestResult.getData();
			productResponseFrontList.forEach(productResponseFront -> {
				GetCmsProductListResponse getCmsProductListResponse = new GetCmsProductListResponse();
				getCmsProductListResponse.setChineseName(productResponseFront.getChineseName());
				getCmsProductListResponse.setMarketPrice(productResponseFront.getMarketPrice());
				getCmsProductListResponse.setPictureUrl(productResponseFront.getPictureUrl());
				getCmsProductListResponse.setProductId(productResponseFront.getId());
				getCmsProductListResponse.setSalePriceWithTax(productResponseFront.getSalePriceWithTax());
				getCmsProductListResponse.setModuleId(getCmsProductListRequest.getModuleId());
				getCmsProductListResponse.setType(getCmsProductListRequest.getType());
				rest.add(getCmsProductListResponse);
			});
			return RestResult.success(rest);
		}catch (Exception e){
			log.error("查询H5Cms配置商品失败============>>>>>{}",e.getLocalizedMessage());
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}

	/**
	 * 新增栏目
	 * @param addCmsModuleRequest
	 * @return
	 */
	@Override
	public RestResult<PageModuleResponse> addModule(AddCmsModuleRequest addCmsModuleRequest) {
		try {
			//查询存在的模板
			Integer store = 0;
			List<CmsModule> cmsModuleList = cmsModuleMapper.query(new CmsModule() {{
				setCmsPageId(addCmsModuleRequest.getPageId());
				setRefType(0);
				setDeleteFlag(0);
			}});
			if (CollectionUtils.isNotEmpty(cmsModuleList)) {
				store = cmsModuleList.get(cmsModuleList.size()-1).getSort()+1;
			}
			Integer finalStore = store;
			CmsModule cmsModule = new CmsModule() {{
				setCmsPageId(addCmsModuleRequest.getPageId());
				setRefType(0);
				setDataType(addCmsModuleRequest.getTemplateDataType());
				setSort(finalStore);
				setPlatform(addCmsModuleRequest.getPlatform());
				setCreateTime(new Date());
				setDeleteFlag(0);
			}};
			cmsModuleMapper.insert(cmsModule);
			//新增栏目模板
			cmsModuleTemplateMapper.insert(new CmsModuleTemplate(){{
				setPreviewImg(addCmsModuleRequest.getPreviewImg());
				setCmsModuleId(cmsModule.getId());
				setCmsTemplateId(addCmsModuleRequest.getTemplateId());
				setCode(addCmsModuleRequest.getTemplateCode());
				setName(addCmsModuleRequest.getTemplateName());
				setDataType(addCmsModuleRequest.getTemplateDataType());
				setPlatform(addCmsModuleRequest.getPlatform());
				setDeleteFlag(0);
				setCreateTime(new Date());
			}});
			PageModuleResponse pageModuleResponse = new PageModuleResponse();
			pageModuleResponse.setModuleId(cmsModule.getId());
			pageModuleResponse.setCode(addCmsModuleRequest.getTemplateCode());
			pageModuleResponse.setSort(cmsModule.getSort());
			pageModuleResponse.setDataType(addCmsModuleRequest.getTemplateDataType());
			pageModuleResponse.setPreviewImg(addCmsModuleRequest.getPreviewImg());
			return RestResult.success(pageModuleResponse);
		}catch (Exception e){
			log.error("新增栏目失败====>>>>>>>>>>>>{}",e.getLocalizedMessage());
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}

	/**
	 * 添加模块数据
	 * @param addModuleDataRequest 添加模块数据
	 * @return String 返回添加模块数据中文信息
	 * author 建投数据科技(山东)有限公司
	 * @date 2020-5-11 10:33:34
	 */
	@Override
	public RestResult<String> addModuleData(AddModuleDataRequest addModuleDataRequest) {
		try {

			if (addModuleDataRequest.getModuleId()==null){
				throw new RuntimeException("模块Id为空");
			}

			Integer moduleDataType = addModuleDataRequest.getTemplateDataType();
			if (moduleDataType == 0){
				//静态数据
				List<CmsModuleTemplate> cmsModuleTemplateList = cmsModuleTemplateMapper.query(new CmsModuleTemplate() {{
					setCmsModuleId(addModuleDataRequest.getModuleId());
					setDeleteFlag(0);
					setPlatform(addModuleDataRequest.getPlatform());
				}});
				if(CollectionUtils.isEmpty(cmsModuleTemplateList)){
					throw new RuntimeException("栏目模板不存在");
				}
				cmsModuleTemplateMapper.update(new CmsModuleTemplate(){{
					setVariables(JSON.toJSONString(addModuleDataRequest.getVariablesObject()));
					setId(cmsModuleTemplateList.get(0).getId());
					setUpdateTime(new Date());
					setPlatform(addModuleDataRequest.getPlatform());
				}});
			}else{
				//动态数据
				addModuleDataRequest.getProductIds().forEach(productId->{
					cmsModuleDataMapper.insert(new CmsModuleData(){{
						setCmsModuleId(addModuleDataRequest.getModuleId());
						setMpId(productId);
						setDeleteFlag(0);
						setCreateTime(new Date());
						setType(addModuleDataRequest.getType());
					}});
				});
			}
			return RestResult.success("成功");
		}catch (Exception e){
			log.error("异常",e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestResult<String> sectionMobile(SectionMobileRequest sectionMobileRequest) {
		try {
			Long moduleId = sectionMobileRequest.getModuleId();
			Long toModuleId = sectionMobileRequest.getToModuleId();
			CmsModule cmsModule = cmsModuleMapper.queryByPrimaryKey(moduleId);
			CmsModule cmsModuleTo = cmsModuleMapper.queryByPrimaryKey(toModuleId);

			cmsModuleMapper.update(new CmsModule(){{
				setId(cmsModule.getId());
				setSort(cmsModuleTo.getSort());
			}});
			cmsModuleMapper.update(new CmsModule(){{
				setId(cmsModuleTo.getId());
				setSort(cmsModule.getSort());
			}});
//			List<CmsModule> cmsModuleList = cmsModuleMapper.query(new CmsModule() {{
//				setCmsPageId(sectionMobileRequest.getPageId());
//				setDeleteFlag(0);
//				setRefType(0);
//			}});
//			CmsModule cmsModule = new CmsModule();
//			CmsModule cmsModuleReplaced = new CmsModule();
//
//			for (int i = 0; i < cmsModuleList.size(); i++) {
//				if (cmsModuleList.get(i).getId().equals(sectionMobileRequest.getModuleId())){
//					//相同 替换
//					cmsModule.setId(cmsModuleList.get(i).getId());
//					cmsModuleReplaced.setSort(cmsModuleList.get(i).getSort());
//					if (sectionMobileRequest.getMobileType() == 1){
//						//查询上一个替换sort
//						cmsModule.setSort(cmsModuleList.get(i-1).getSort());
//						cmsModuleReplaced.setId(cmsModuleList.get(i-1).getId());
//					}else{
//						//查询下一个
//						cmsModule.setSort(cmsModuleList.get(i+1).getSort());
//						cmsModuleReplaced.setId(cmsModuleList.get(i+1).getId());
//					}
//				}
//			}
//			//修改sort
//			cmsModuleMapper.update(cmsModule);
//			cmsModuleMapper.update(cmsModuleReplaced);
			return RestResult.success("成功");
		}catch (Exception e){
			log.error("移动栏目错误===>>>>{}",e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}

	@Override
	public RestResult<CmsGetIndexPageConfResponse> getModuleCmsConf(GetModuleCmsConfRequest getModuleCmsConfRequest) {
		try {
			//查询生效的page
			CmsPage cmsPage = cmsPageMapper.queryByPrimaryKey(getModuleCmsConfRequest.getPageId());
			//查询module list
			Optional.ofNullable(cmsPage).orElseThrow(() -> new AppRuntimeException(ResponseCode.EXCEPTION, "不存在生效的CMS配置页面"));
			List<PageModuleResponse> pageModuleResponseList = cmsModuleMapper.queryListByCmsPageId(cmsPage.getId());
			List<GetCmsProductListResponse> cmsProductListRes = new ArrayList<>();
			pageModuleResponseList.forEach(pageModuleResponse -> {

				List<GetCmsProductListResponse> itemProductList = new ArrayList<>();
				if (StringUtils.isNotEmpty(pageModuleResponse.getVariables())){
					pageModuleResponse.setVariablesObject(JSONArray.parseObject(pageModuleResponse.getVariables(),Object.class));
				}

				RestResult<List<GetCmsProductListResponse>> cmsProductList = this.getCmsProductList(new GetCmsProductListRequest() {{
					setModuleId(pageModuleResponse.getModuleId());
					setType(0);
				}});

				if (cmsProductList.getCode().equalsIgnoreCase("0000") && cmsProductList.getData()!=null){
					cmsProductListRes.addAll(cmsProductList.getData());
					itemProductList.addAll(cmsProductList.getData());
				}
				RestResult<List<GetCmsProductListResponse>> cmsProductList2 = this.getCmsProductList(new GetCmsProductListRequest() {{
					setModuleId(pageModuleResponse.getModuleId());
					setType(1);
				}});

				if (cmsProductList.getCode().equalsIgnoreCase("0000") && cmsProductList2.getData()!=null){
					cmsProductListRes.addAll(cmsProductList2.getData());
					itemProductList.addAll(cmsProductList2.getData());
				}
				pageModuleResponse.setCmsProductList(itemProductList);
			});
			CmsGetIndexPageConfResponse cmsGetIndexPageConfResponse = new CmsGetIndexPageConfResponse();
			cmsGetIndexPageConfResponse.setPageId(cmsPage.getId()); //页面id
			cmsGetIndexPageConfResponse.setTitle(cmsPage.getTitle()); //页面名称
			cmsGetIndexPageConfResponse.setShareDesc(cmsPage.getShareDesc()); //页面分享描述
			cmsGetIndexPageConfResponse.setShareImg(cmsPage.getShareImg()); //分享图片
			cmsGetIndexPageConfResponse.setPageModuleResponseList(pageModuleResponseList); //页面栏
			cmsGetIndexPageConfResponse.setCmsProductList(cmsProductListRes); //页面栏

			return RestResult.success(cmsGetIndexPageConfResponse);
		}catch (Exception e){
			log.error("查询H5Cms配置失败============>>>>>{}");
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}
	@Override
	public RestResult<CmsModuleTemplate> getCmsModuleTemplate(CmsModuleTemplateQuery request) {
		try {
			//查询生效的page
			CmsModuleTemplate query = new CmsModuleTemplate();
			query.setCmsModuleId(request.getModuleId());
			List<CmsModuleTemplate> list = cmsModuleTemplateMapper.query(query);
			if (list == null || list.size()!=1){
				throw new AppRuntimeException(ResponseCode.EXCEPTION);
			}
			//查询module list
			return RestResult.success(list.get(0));
		}catch (Exception e){
			log.error("查询H5Cms配置失败============>>>>>{}");
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}

	@Override
	public RestResult<String> deleteModule(SectionMobileRequest sectionMobileRequest) {
		try{
			cmsModuleMapper.delete(sectionMobileRequest.getModuleId());
			return RestResult.success("成功");
		}catch (Exception e){
			log.error("删除栏目失败==>>>{}",e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}

	/**
	 * 记录详情
	 *
	 * @param request
	 * @return
	 */
	@Override
	public CmsModule getCmsModule(CmsModule request) {
		return cmsModuleMapper.selectByPrimaryKey(request);
	}


	/**
	 * 新增记录
	 *
	 * @param request
	 * @return
	 */
	@Override
	public long addCmsModule(CmsModule request) {
		return cmsModuleMapper.insert(request);
	}


	/**
	 * 删除记录
	 *
	 * @param request
	 * @return
	 */
	@Override
	public long deleteCmsModule(CmsModule request) {
		return cmsModuleMapper.deleteByPrimaryKey(request);
	}


	/**
	 * 逻辑删除记录
	 *
	 * @param request
	 * @return
	 */
	@Override
	public long deleteCmsModuleLogically(CmsModule request) {
		request.setDeleteFlag(1);
		return cmsModuleMapper.updateByPrimaryKey(request);
	}


	/**
	 * 保存或修改记录信�
	 * @param request 入参实体�
	 * @return String
	 * author tzc
	 * @date 2020-3-11 20:54:09
	 */
	@Override
	public long saveOrUpdateCmsModule(CmsModule request) throws Throwable {
		try {
			Optional.ofNullable(request).orElseThrow(() -> new AppRuntimeException(ResponseCode.EXCEPTION, "不存在生效的CMS配置页面"));
			//if (StringUtils.isBlank(request.getInstitutionCode())) {
			//return RestResult.error(ChoiceCenterErrorCode.EXCEPTION, "营业执照为空");
			//}
			request.setDeleteFlag(0);
			request.setCreator(request.getCreator());
			if (request !=null && request.getId() != null) {
				return  cmsModuleMapper.updateByPrimaryKey(request);
			}else{
				return cmsModuleMapper.insertSelective(request);
			}
		} catch (Exception e) {
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
	}
}